package com.one.blocks.common.valid.validator;

import com.one.blocks.common.valid.constraints.EnumContains;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author linfuxin Created on 2022-08-16 17:41:36
 */
public class EnumContainsValidatorForInteger implements ConstraintValidator<EnumContains, Integer> {

    private Class<? extends Enum<? extends EnumContainsValid<Integer>>> enumClass;

    @Override
    public void initialize(EnumContains constraintAnnotation) {
        //noinspection unchecked
        this.enumClass = (Class<? extends Enum<? extends EnumContainsValid<Integer>>>) constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        //noinspection unchecked
        EnumContainsValid<Integer>[] enums = (EnumContainsValid<Integer>[]) enumClass.getEnumConstants();
        return enums != null && enums.length != 0 && enums[0].contains(value);
    }
}
